﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Configuration;
using System.Data.SqlClient;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Dynamic;
using System.Data;
using Trade.DBUtility;
using Trade.DBUtility.DataTableName;
using System.IO;

namespace TradeWeb.Controllers
{
    public class UserController : Controller
    {
        //检测用户代码是否存在
        public string CheckUserExistByUserCode(string strUserCode)
        {
            string jsonResult = string.Empty;
            //构造反馈对象
            dynamic damResult = new ExpandoObject();
            damResult.success = false;
            damResult.message = "获取失败";

            DataSet dsUser = null;
            //
            DataTableName DataTableNamer = new DataTableName();
            DataTableNamer.strSelectSQL = "SELECT bb.CNDeptName,aa.UserCode,aa.CNUserName,aa.ENUserName,aa.ParentDeptCode,aa.OTel,aa.OFax,aa.OMobile,aa.OMail,aa.Remark FROM dbo.T_User aa LEFT JOIN dbo.T_Dept bb ON aa.ParentDeptCode=bb.DeptCode WHERE aa.ISDel=0 And aa.UserCode=@UserCode";
            DataTableNamer.SqlSelectParameterList.Clear();
            DataTableNamer.SqlSelectParameterList.Add(new SqlParameter("@UserCode", strUserCode));
            DataTableNamer.strTableName = "T_User";
            try
            {
                dsUser = CustomizableDesignBLL.GetData(DataTableNamer, false);
            }
            catch (Exception)
            {
                damResult.message = "查询失败";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            if (dsUser == null)
            {
                damResult.message = "系统未查询到相关数据";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            if (dsUser.Tables["T_User"].DefaultView.Count > 0)
            {
                damResult.success =true;
                damResult.data = "存在";
                damResult.message = "正常";
                jsonResult = JsonConvert.SerializeObject(damResult);
            }
            else
            {
                damResult.success = true;
                damResult.data = "不存在";
                damResult.message = "正常";
                jsonResult = JsonConvert.SerializeObject(damResult);
            }
            return jsonResult;
        }
        public string checkCJAppCode(string BDCode)
        {
            string jsonResult = string.Empty;
            //构造反馈对象
            dynamic damResult = new ExpandoObject();
            damResult.success = false;
            damResult.message = "获取失败";
            //
            DataSet dsAppVison = null;
            //
            DataTableName DataTableNamer = new DataTableName();
            DataTableNamer.strSelectSQL = "SELECT  * FROM dbo.T_AppNewViSionCJ";
            DataTableNamer.strTableName = "T_AppNewViSionCJ";
            try
            {
                dsAppVison = CustomizableDesignBLL.GetData(DataTableNamer, false);
            }
            catch (Exception)
            {
                damResult.message = "查询最新版本号失败";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            decimal bdcode = decimal.Parse(BDCode);
            decimal newCode = decimal.Parse(dsAppVison.Tables[0].DefaultView[0]["VisionCode"].ToString());
            if (bdcode == newCode)
            {
                damResult.success = true;
                damResult.message = "无更新";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            else
            {
                damResult.success = true;
                damResult.VisonName = dsAppVison.Tables[0].DefaultView[0]["VisionCode"].ToString();
                damResult.message = dsAppVison.Tables[0].DefaultView[0]["UpdateContent"].ToString();
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
        }
        //获取用户信息
        public string GetLoginUserInfoByUserCode(string strUserCode)
        {

            string jsonResult = string.Empty;
            //构造反馈对象
            dynamic damResult = new ExpandoObject();
            damResult.success = false;
            damResult.message = "获取失败";
            DataSet dsUser = null;

            DataTableName DataTableNamer = new DataTableName();
            DataTableNamer.strSelectSQL = "SELECT bb.CNDeptName,aa.UserCode,aa.CNUserName,aa.ENUserName,aa.ParentDeptCode FROM dbo.T_User aa JOIN dbo.T_Dept bb ON aa.ParentDeptCode=bb.DeptCode WHERE aa.ISDel=0 And aa.UserCode=@UserCode";
            DataTableNamer.SqlSelectParameterList.Clear();
            DataTableNamer.SqlSelectParameterList.Add(new SqlParameter("@UserCode", strUserCode));
            DataTableNamer.strTableName = "T_User";
            try
            {
                dsUser = CustomizableDesignBLL.GetData(DataTableNamer, false);
            }
            catch (Exception)
            {
                damResult.message = "查询失败";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            if (dsUser == null)
            {
                damResult.message = "系统未查询到相关数据";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            try
            {
                List<dynamic> varList = new List<dynamic>();
                foreach (DataRowView drv in dsUser.Tables[0].DefaultView)
                {
                    var item = Trade.DBUtility.ExtendObject.AutoMap(drv.Row);
                    varList.Add(item);
                }
                damResult.success = true;
                damResult.data = varList;
                damResult.message = "正常";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            catch (Exception)
            {
                damResult.message = "查询结果格式不正确";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
        }
        //用户名+密码登陆并获取+用户信息
        public string Login(string strUserCode, string strPassWord)
        {
            string jsonResult = string.Empty;
            //构造反馈对象
            dynamic damResult = new ExpandoObject();
            damResult.success = false;
            damResult.message = "获取失败";
            DataSet dsUser = null;
            DataTableName DataTableNamer = new DataTableName();
            DataTableNamer.strSelectSQL = "SELECT bb.CNDeptName,aa.UserCode,aa.CNUserName,aa.ENUserName,aa.ParentDeptCode,aa.OTel,aa.OFax,aa.OMobile,aa.OMail,aa.Remark FROM dbo.T_User aa JOIN dbo.T_Dept bb ON aa.ParentDeptCode=bb.DeptCode WHERE aa.ISDel=0 And aa.UserCode=@UserCode and aa.MD5Password=@MD5Password";
            DataTableNamer.SqlSelectParameterList.Clear();
            DataTableNamer.SqlSelectParameterList.Add(new SqlParameter("@UserCode", strUserCode));
            DataTableNamer.SqlSelectParameterList.Add(new SqlParameter("@MD5Password", strPassWord));
            DataTableNamer.strTableName = "T_User";
            try
            {
                dsUser = CustomizableDesignBLL.GetData(DataTableNamer, false);
            }
            catch (Exception)
            {
                damResult.message = "查询失败";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            if (dsUser == null)
            {
                damResult.message = "系统未查询到相关数据";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            try
            {
                List<dynamic> varList = new List<dynamic>();
                foreach (DataRowView drv in dsUser.Tables[0].DefaultView)
                {
                    var item = Trade.DBUtility.ExtendObject.AutoMap(drv.Row);
                    varList.Add(item);
                }
                damResult.success = true;
                damResult.data = varList;
                damResult.message = "正常";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            catch (Exception)
            {
                damResult.message = "查询结果格式不正确";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
        }

        //用户名+密码登陆并获取+用户信息
        public string LoginYY(string strUserCode, string strPassWord, string strServer, string strZT)
        {
            //string strPath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/Content/") + "log_" + DateTime.Now.ToString("yyyyMMdd") + ".ini");
            //new YYAPI.Log().LogWrite(strPath, "日志0：");
            //先插入数据
            //
            string jsonResult = string.Empty;
            //构造反馈对象
            dynamic damResult = new ExpandoObject();
            damResult.success = false;
            damResult.message = "获取失败";
            //

            DataTableName dtn = new DataTableName();
            dtn.strExecSQL = "INSERT INTO T_User(UserCode,ISDel,CNUserName,ENUserName,ParentDeptCode,ISExport,ISViewStockPrice,ISViewSalePrice,ISUse,ISSys,MD5Password) SELECT cUser_Id UserCode,0 ISDel,cUser_Name CNUserName,cUser_Name ENUserName,'001' ParentDeptCode,'是' ISExport,'是' ISViewStockPrice,'是' ISViewSalePrice,'是' ISUse,'否' ISSys,'D41D8CD98F00B204E9800998ECF8427E' MD5Password FROM " + Common.GetdatabaseName(strZT) + ".dbo.ua_user where cUser_Id not in (select usercode from t_user) ";
            try
            {
                CustomizableDesignBLL.ExecSQL(dtn);
            }
            catch (Exception)
            {
                //jsonResult = "{\"success\":false,\"data\":[],\"message\":\"同步用户失败！\"}";
                //return jsonResult;
                damResult.message = "同步用户失败";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            //
            DataSet dsUser = null;
            DataTableName DataTableNamer = new DataTableName();
            DataTableNamer.strSelectSQL = "SELECT bb.CNDeptName,aa.UserCode,aa.CNUserName,aa.ENUserName,aa.ParentDeptCode,aa.OTel,aa.OFax,aa.OMobile,aa.OMail,aa.Remark FROM dbo.T_User aa JOIN dbo.T_Dept bb ON aa.ParentDeptCode=bb.DeptCode WHERE aa.ISDel=0 And aa.UserCode=@UserCode";
            DataTableNamer.SqlSelectParameterList.Clear();
            DataTableNamer.SqlSelectParameterList.Add(new SqlParameter("@UserCode", strUserCode));
            DataTableNamer.SqlSelectParameterList.Add(new SqlParameter("@MD5Password", strPassWord));
            DataTableNamer.strTableName = "T_User";
            try
            {
                dsUser = CustomizableDesignBLL.GetData(DataTableNamer, false);
            }
            catch (Exception ex)
            {
                damResult.message = "查询用户数据失败报错:" + ex.Message;
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            if (dsUser == null)
            {
                damResult.message = "系统未查询到相关数据";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            //new YYAPI.Log().LogWrite(strPath, "日志1：");
            //
            YYAPI.YongYouAPI YYAPI = new YYAPI.YongYouAPI();
            string strYongYouDBName = Common.GetdatabaseName(strZT);
            string sSubId = "AS";
            string sAccID = strZT;
            DateTime dtCreateDate = DateTime.Now;
            string sYear = dtCreateDate.ToString("yyyy");
            //
            string sUserID = strUserCode;
            string sPassword = strPassWord;
            string sDate = dtCreateDate.ToString("yyyy-MM-dd");
            string sServer = Common.getU8SerVerIP();// strServer; 
            string sSerial = "";
            //
            string strRet = YYAPI.u8Login(sSubId, sAccID, sYear, sUserID, sPassword, sDate, sServer, sSerial);
            //new YYAPI.Log().LogWrite(strPath, "日志2："); 
            if (strRet != string.Empty)
            {
                damResult.message = strRet;
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            try
            {
                List<dynamic> varList = new List<dynamic>();
                foreach (DataRowView drv in dsUser.Tables[0].DefaultView)
                {
                    var item = Trade.DBUtility.ExtendObject.AutoMap(drv.Row);
                    varList.Add(item);
                }
                //new YYAPI.Log().LogWrite(strPath, "日志3：");
                damResult.success = true;
                damResult.message = "正常";
                damResult.data = varList;
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;

            }
            catch (Exception)
            {
                damResult.message = "查询结果格式不正确";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
        }
        //返回最新的版本号
        public string CheckAppVersionCode()
        {
            string jsonResult = string.Empty;
            //构造反馈对象
            dynamic damResult = new ExpandoObject();
            damResult.success = false;
            damResult.message = "获取失败";
            //
            DataSet dsAppVison = null;
            //
            DataTableName DataTableNamer = new DataTableName();
            DataTableNamer.strSelectSQL = "SELECT  * FROM dbo.T_AppNewViSion ";
            DataTableNamer.strTableName = "T_AppNewViSion";
            try
            {
                dsAppVison = CustomizableDesignBLL.GetData(DataTableNamer, false);
            }
            catch (Exception)
            {

                damResult.message = "查询最新版本号失败";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            if (dsAppVison == null)
            {
                damResult.message = "系统未查询到查询最新版本信息";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            if (dsAppVison.Tables["T_AppNewViSion"].DefaultView.Count > 0)
            {
                damResult.success = true;
                damResult.VisonName = dsAppVison.Tables["T_AppNewViSion"].DefaultView[0]["VerSionCode"].ToString();
                damResult.message = dsAppVison.Tables["T_AppNewViSion"].DefaultView[0]["NewVersionMsg"].ToString();
                jsonResult = JsonConvert.SerializeObject(damResult);

            }
            else
            {
                damResult.message = "没有最新版本信息";
                jsonResult = JsonConvert.SerializeObject(damResult);
                return jsonResult;
            }
            //
            jsonResult = JsonConvert.SerializeObject(damResult);
            return jsonResult;
        }

    }
}
